// pages/ProductDetail/ProductDetail.js
const ProductDetail = require('../../utils/ProductDetail');
const KindHttp = require('../../utils/KindHttp');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        item: [],
        shopNum: 0,
        active: 0,
        showOne: false,
        showShare: false,
        options: [
            { name: '微信', icon: 'wechat', openType: 'share' },
            { name: '微博', icon: 'weibo' },
            { name: '复制链接', icon: 'link' },
            { name: '分享海报', icon: 'poster' },
            { name: '二维码', icon: 'qrcode' },
        ],
        steps: [
            {
                text: '',
                desc: '买家下单',
                inactiveIcon: 'location-o',
                activeIcon: 'success',
            },
            {
                text: '',
                desc: '商家接单',
                inactiveIcon: 'like-o',
                activeIcon: 'success',
            },
            {
                text: '',
                desc: '商家发货',
                inactiveIcon: 'star-o',
                activeIcon: 'success',
            },
            {
                text: '',
                desc: '买家收货',
                inactiveIcon: 'phone-o',
                activeIcon: 'success',
            },
        ],
    },
    handleShop() {
        wx.switchTab({
            url: '/pages/ShopIndex/shopping/shopping',
        })
    },
    handleStep(e) {
        this.setData({
            active: e.detail
        })
    },
    onFxClick(event) {
        this.setData({ showShare: true });
    },

    onClose() {
        this.setData({ showShare: false });
    },

    onSelect(event) {
        // Toast(event.detail.name);
        this.onClose();
    },
    handleClickOneList() {
        this.setData({ showOne: true });
    },
    onCloseOne() {
        this.setData({ showOne: false });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        var { id } = options;
        var res = await ProductDetail.getProductDetail(id);
        this.setData({
            item: res.data.res[0]
        })
    },
    async onClick(e) {
        var { aid, collect } = e.currentTarget.dataset
        if (collect === false) {
            wx.request({
                url: 'http://122.112.142.6:8888/api/kindCollect',
                data: {
                    id: aid,
                    collect: true
                },
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: async (res) => {
                    if (res.data.msg == "收藏成功" && res.data.status == 200) {
                        wx.showToast({
                            title: '加入购物车成功 !',
                            icon: 'none',
                        })
                    } else {
                        wx.showToast({
                            title: '已经在购物车中了',
                            icon: 'none',
                        })
                    }
                    var userCollRes = await KindHttp.getUserCollect();
                    // console.log(userCollRes.data.res);
                    this.setData({
                        shopNum: userCollRes.data.res.length
                    })
                },
            })

        } else {
            // wx.request({
            //     url: 'http://122.112.142.6:8888/api/kindCollect',
            //     data: {
            //         id: aid,
            //         collect: false
            //     },
            //     header: {
            //         "Content-Type": "application/x-www-form-urlencoded"
            //     },
            //     method: 'POST',
            //     success: (res) => {
            //         if (res.data.msg == "取消收藏") {
            //             let removeToPull = true;
            //             this.triggerEvent('removeToPull', removeToPull)
            //             wx.showToast({
            //                 title: '取消加入成功 !',
            //                 icon: 'none',
            //             })
            //         }
            //     },
            // })
            wx.showToast({
                title: '已经在购物车中了',
                icon: 'none',
            })

        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        var userCollRes = await KindHttp.getUserCollect();
        // console.log(userCollRes.data.res);
        this.setData({
            shopNum: userCollRes.data.res.length
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})